import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;




@SuppressWarnings("serial")
public class FrameAlteraCliente extends JFrame {

	private FramePrincipal fp ;
	private Cliente cliente ;
	
	private JTextField fieldNome ;
	private JTextField fieldTelefone;
	
	private PreparedStatement pst ;
	
	
	public FrameAlteraCliente(FramePrincipal framep, Cliente c) {
		fp = framep;
		cliente = c ;
		
		setSize(800 / 3, 600 / 3);
		setResizable(false);
		setLocationRelativeTo(fp);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("Novo cliente");

		JPanel painel = new JPanel();
		getContentPane().add(painel);
		painel.setLayout(null);

		fieldNome = new JTextField();
		fieldNome.setBounds(10, 36, 226, 20);
		painel.add(fieldNome);
		fieldNome.setColumns(10);
		fieldNome.setText(c.getNome());
		
		fieldTelefone = new JTextField();
		fieldTelefone.setBounds(10, 85, 226, 20);
		painel.add(fieldTelefone);
		fieldTelefone.setColumns(10);
		fieldTelefone.setText(Integer.toString(c.getTelefone()));
		
		JLabel labelUsuario = new JLabel("Nome");
		labelUsuario.setBounds(10, 22, 46, 14);
		painel.add(labelUsuario);

		JLabel labelSenha = new JLabel("Telefone");
		labelSenha.setBounds(10, 72, 105, 14);
		painel.add(labelSenha);
	
		JButton botaoOk = new JButton("OK");
		botaoOk.setBounds(145, 127, 89, 23);
		painel.add(botaoOk);
		
		botaoOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String n, temp ;
				int t ;
				n = fieldNome.getText().trim();
				if (!n.equals("")) {
					try {
						temp = fieldTelefone.getText().trim();
						t = Integer.parseInt(temp);
						pst = fp.conn.prepareStatement("UPDATE Cliente SET nome = ?, telefone = ? WHERE ID = ?");
						pst.setString(1, n);
						pst.setInt(2, t);
						pst.setInt(3, cliente.getId());
						pst.execute();
						pst.close();
						cliente.setNome(n);
						cliente.setTelefone(t);
						fp.clienteModel.fireTableDataChanged();
						dispose();
					} catch (Exception ex) {
						JOptionPane.showMessageDialog(null,"Digite um valor valido no campo telefone") ;
					}
				} else JOptionPane.showMessageDialog(null, "Digite um valor valido no campo nome"); 				
			}
		}) ;
	
		setVisible(true);
	}
}
